//
//  YHMeOrderSubViewController.m
//  YHApp
//
//  Created by -- on 2021/8/9.
//

#import "HAOrderSubVC.h"
#import "HAOrderListCell.h"
#import "HAOrderMo.h"
#import "HAOrderDVC.h"

@interface HAOrderSubVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>


@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;

@end

@implementation HAOrderSubVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    MJRefreshGifHeader *mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header = mj_header;
    
    MJRefreshBackGifFooter *mj_footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(laodMoreData)];
    self.tableView.mj_footer = mj_footer;
    
    [self.tableView.mj_header beginRefreshing];
    
    // Do any additional setup after loading the view.
}

-(void)setupUI {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 92+32+5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HAOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMeOrderListCellID"];
    if (!cell) {
        cell = [[HAOrderListCell alloc]initWithStyle:0 reuseIdentifier:@"YHMeOrderListCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.isD = YES;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HAOrderMo *model = self.dataArray [indexPath.row];
    HAOrderDVC *vc = [[HAOrderDVC alloc]init];
    vc.order_id = model.ID;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)loadNewData {
    
    NSDictionary *dict = @{
        @"pageSize":@"10",
        @"page":@"1",
        @"source_id":self.source_id?:@"",
    };
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"order/items"];
    request1.requestMethod = SYRequestMethodGET;
    request1.parms = dict;

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            [self.tableView.mj_header endRefreshing];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [self.dataArray removeAllObjects];
                if ([[dataModel.data allKeys]containsObject:@"data"]) {
                    NSArray *arr = [HAOrderMo mj_objectArrayWithKeyValuesArray:[dataModel.data valueForKey:@"data"]];
                    [self.dataArray addObjectsFromArray:arr];
                }
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
            
            [self.tableView reloadData];
        }
    }];
    
}

-(void)laodMoreData {
    
    NSInteger count = ceilf(self.dataArray.count*0.1)+1;
    NSString *pageNo = [NSString stringWithFormat:@"%ld",(long)count];
    
    NSDictionary *dict = @{
        @"pageSize":@"10",
        @"page":pageNo,
        @"source_id":self.source_id?:@"",
    };
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"order/items"];
    request1.requestMethod = SYRequestMethodGET;
    request1.parms = dict;

    [self.tableView.mj_footer beginRefreshing];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            [self.tableView.mj_footer endRefreshing];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                if ([[dataModel.data allKeys]containsObject:@"data"]) {
                    NSArray *arr = [HAOrderMo mj_objectArrayWithKeyValuesArray:[dataModel.data valueForKey:@"data"]];
                    [self.dataArray addObjectsFromArray:arr];
                }
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
            
            [self.tableView reloadData];
        }
    }];
    
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;

        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



#pragma mark - 无数据处理 DZNEmptyDataSetSource DZNEmptyDataSetDelegate
//设置title
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无数据~";
    NSDictionary *attributes = @{
        NSFontAttributeName:[UIFont systemFontOfSize:14],
        NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]
    };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

//设置image
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"hl_nodata"];
}


@end
